package com.dong.drool.autoconfigure.core;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaodongchao
 * @date 2019/2/28 16:43
 */
public class DroolFileUtil {

    private DroolFileUtil(){}
    /**
     * 从drl文件中读取drl规则到字符串中
     * @param fileName 文件路径名
     * @return 字符串形式的drl规则
     */
    public static String readDrlToStr(String fileName) {
        BufferedReader bReader = null;
        try {
            bReader = new BufferedReader(new FileReader(new File(fileName)));
            StringBuilder sb = new StringBuilder();
            String s ;
            while((s = bReader.readLine()) != null) {
                sb.append(s).append("\n");
            }

            bReader.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bReader != null){
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return "";
    }
    public static String readDrlToStr(File ruleFile){
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(ruleFile)) ;
            StringBuilder sb = new StringBuilder();
            String str ;
            while ( (str = reader.readLine()) != null){
                sb.append(str).append("\n");
            }
            return sb.toString() ;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "" ;
    }
    /**
     * 将给定项目路径下的所有drl规则封装到RuleEntity中
     * @param path 项目中文件夹的路径
     * @return 文件实体集合
     */
    public static List<RuleEntity> getClassPathRules(String path){
        Resource ruleDic = new ClassPathResource(path);
        List<RuleEntity> results = new ArrayList<>();
        List<File> allRules = new ArrayList<>();
        File[] ruleFiles ;
        try {
            ruleFiles =  ruleDic.getFile().listFiles();
        } catch (IOException e) {
            System.out.println(DroolFileUtil.class.getName()+":"+e.getMessage());
            return null ;
        }
        getFiles(ruleFiles,allRules);

        allRules.forEach(ruleFile ->{
            String ruleFileName = ruleFile.getName() ;
            if (ruleFileName.endsWith(".drl")){
                RuleEntity rule = new RuleEntity();
                rule.setRuleName(ruleFileName.substring(0,ruleFileName.lastIndexOf(".")));
                rule.setContent(DroolFileUtil.readDrlToStr(ruleFile));
                results.add(rule);
            }
        });
        return results ;
    }
    private static void getFiles(File[] files, List<File> rules){
        if (null == files || files.length == 0){
            return;
        }
        for (File file : files){
            if (file.isFile()){
                System.out.println("file-->"+file.getName());
                rules.add(file);
            }
            if (file.isDirectory()){
                System.out.println("dic-->"+file.getName());
                getFiles(file.listFiles(),rules);
            }
        }
    }
}
